
var o = require("psconfig"),
    i = function (t) {
        return t[Math.floor(Math.random() * t.length)];
    };
module.exports = {
    grid2Pos: function (t, e) {
        var n = .5 * o.cellSize + (o.cellSize + 2) * e,
            i = .5 * o.cellSize + (o.cellSize + 2) * t;
        return cc.v2(n, i);
    },
    pos2Grid: function (t, e) {
        var n = (e - .5 * o.cellSize) / (o.cellSize + 2),
            i = (t - .5 * o.cellSize) / (o.cellSize + 2);
        return cc.v2(Math.round(n), Math.round(i));
    },
    initMatrixDataPortraitRandom: function () {
        for (var t = new Array(o.matrixRow), e = 0; e < o.matrixRow; e++) {
            t[e] = new Array(o.matrixCol);
            for (var n = 0; n < o.matrixCol; n++) t[e][n] = i(o.totalColors);
        }
        return t;
    },
    indexValue: function (t, e) {
        return t * o.matrixCol + e;
    },
    indexOfV2: function (t, e) {
        return t.some(function (t, n, o) {
            return t.x === e.x && t.y === e.y;
        });
    },
    resolveIndex: function (t) {
        var e = t % o.matrixCol,
            n = (t - e) / o.matrixCol;
        return cc.v2(n, e);
    },
    removeStarsList: function (t, e, n) {
        var i = [],
            a = [];
        a.push(n);
        var s = e[n.x][n.y];
        do {
            var c = a.pop();
            if (c.y - 1 >= 0 && s === e[c.x][c.y - 1]) {
                var r = cc.v2(c.x, c.y - 1);
                this.indexOfV2(a, r) || this.indexOfV2(i, r) || a.push(r);
            }
            c.y + 1 < o.matrixCol && s === e[c.x][c.y + 1] && (r = cc.v2(c.x, c.y + 1), this.indexOfV2(a, r) || this.indexOfV2(i, r) || a.push(r)),
                c.x - 1 >= 0 && s === e[c.x - 1][c.y] && (r = cc.v2(c.x - 1, c.y), this.indexOfV2(a, r) || this.indexOfV2(i, r) || a.push(r)),
                c.x + 1 < o.matrixRow && s === e[c.x + 1][c.y] && (r = cc.v2(c.x + 1, c.y), this.indexOfV2(a, r) || this.indexOfV2(i, r) || a.push(r)),
                i.push(c), t[this.indexValue(c.x, c.y)].getComponent("Star").selected.node.active = !0;
        } while (a.length > 0);
        return i.length <= 1 && (t[this.indexValue(n.x, n.y)].getComponent("Star").selected.node.active = !1),
            i;
    },
    needCheckCols: function (t) {
        var e = [];
        return t.forEach(function (t, n, o) {
            -1 == e.indexOf(t.y) && e.push(t.y);
        }), e.sort(function (t, e) {
            return e - t;
        }), e;
    },
    gameOver: function (t) {
        for (var e = 0; e < o.matrixRow; e++)
            for (var n = 0; n < o.matrixCol; n++) {
                var i = t[e][n];
                if (i >= 0) {
                    var a = cc.v2(e, n);
                    if (a.y - 1 >= 0 && i == t[a.x][a.y - 1]) return !1;
                    if (a.y + 1 < o.matrixCol && i == t[a.x][a.y + 1]) return !1;
                    if (a.x - 1 >= 0 && i == t[a.x - 1][a.y]) return !1;
                    if (a.x + 1 < o.matrixRow && i == t[a.x + 1][a.y]) return !1;
                }
            }
        return !0;
    },
    getProgress: function (t, e) {
        return cc.misc.clamp01(t / e);
    }
}